﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using System.Data;
using EMPower.Utility;

public partial class User_MemberRelation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.errorId.Visible = false;
            BindNomalUserList();
            BindAngelUserList();
            BindRelationInfo();
        }
    }

    private void BindNomalUserList()
    {
        ListItem item = new ListItem();
        item.Text = "---请选择自强会员---";
        item.Value = "-1";
        this.NomalUserDownList.Items.Add(item);

        List<UserPassportInfo> nomalUserList = BLL.BUserPassport.getInstance().GetUserPassportInfoByUserType(1);
        if (nomalUserList != null && nomalUserList.Count > 0)
        {
            foreach (UserPassportInfo userInfo in nomalUserList)
            {
                ListItem item1 = new ListItem();
                item1.Text = userInfo.UserName;
                item1.Value = Convert.ToString(userInfo.UserPassportId);
                this.NomalUserDownList.Items.Add(item1);
            }
        }
    }

    private void BindAngelUserList()
    {
        ListItem item = new ListItem();
        item.Text = "---请选择天使会员---";
        item.Value = "-1";
        this.AngelUserDownList.Items.Add(item);

        List<UserPassportInfo> angelUserList = BLL.BUserPassport.getInstance().GetUserPassportInfoByUserType(2);
        if (angelUserList != null && angelUserList.Count > 0)
        {
            foreach (UserPassportInfo userInfo in angelUserList)
            {
                ListItem item1 = new ListItem();
                item1.Text = userInfo.UserName;
                item1.Value = Convert.ToString(userInfo.UserPassportId);
                this.AngelUserDownList.Items.Add(item1);
            }
        }
    }

    private void BindRelationInfo()
    {
        // 获取所有会员列表
        List<UserPassportInfo> userInfoList = BLL.BUserPassport.getInstance().GetUserPassportInfoByStatus(1);
        Dictionary<long, string> userDic = new Dictionary<long, string>();
        if (userInfoList != null && userInfoList.Count > 0)
        {
            foreach (UserPassportInfo userPassportInfo in userInfoList)
            {
                long userPassportId = userPassportInfo.UserPassportId;
                string userName = userPassportInfo.UserName;
                if (!userDic.ContainsKey(userPassportId))
                {
                    userDic.Add(userPassportId, userName);
                }
            }
        }
        // 获取关系会员列表
        List<UserRelationInfo> userRelationInfoList = BLL.BUserRelation.getInstance().GetAllRelationInfo();

        DataTable table = new DataTable();
        DataColumn dc1 = new DataColumn("UserRelationId");
        DataColumn dc2 = new DataColumn("NomalUserName");
        DataColumn dc3 = new DataColumn("AngelUserName");
        DataColumn dc4 = new DataColumn("DateCreated");
        DataColumn dc5 = new DataColumn("NormalUserPassportId");
        DataColumn dc6 = new DataColumn("AngelUserPassportId");
        table.Columns.Add(dc1);
        table.Columns.Add(dc2);
        table.Columns.Add(dc3);
        table.Columns.Add(dc4);
        table.Columns.Add(dc5);
        table.Columns.Add(dc6);
        if (userRelationInfoList != null && userRelationInfoList.Count > 0)
        {
            foreach (UserRelationInfo userRelationInfo in userRelationInfoList)
            {
                DataRow dr = table.NewRow();
                dr["UserRelationId"] = userRelationInfo.UserRelationId;
                dr["NomalUserName"] = userDic.ContainsKey(userRelationInfo.NormalUserPassportId) ? userDic[userRelationInfo.NormalUserPassportId] : "";//userRelationInfo.UserRelationId;
                dr["AngelUserName"] = userDic.ContainsKey(userRelationInfo.AngelUserPassportId) ? userDic[userRelationInfo.AngelUserPassportId] : "";//userRelationInfo.UserRelationId;
                dr["DateCreated"] = userRelationInfo.DateCreated;
                dr["NormalUserPassportId"] = userRelationInfo.NormalUserPassportId;
                dr["AngelUserPassportId"] = userRelationInfo.AngelUserPassportId;
                table.Rows.Add(dr);
            }
        }
        this.RelationInfoDataGrid.DataSource = table;
        this.RelationInfoDataGrid.DataBind();
    }

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        this.RelationInfoDataGrid.CurrentPageIndex = 0;
        string nomalUserPassportId = this.NomalUserDownList.SelectedValue;
        string angelUserPassportId = this.AngelUserDownList.SelectedValue;

        if ("-1".Equals(nomalUserPassportId) || "-1".Equals(angelUserPassportId))
        {
            this.errorId.Text = "请选择自强会员和天使会员";
            this.errorId.Visible = true;
            return;
        }

        // 判断是否已经含有关联关系
        UserRelationInfo relationInfo = BLL.BUserRelation.getInstance().GetRelationInfoByPassportId(nomalUserPassportId, angelUserPassportId);
        if (relationInfo == null || relationInfo.UserRelationId == 0)
        {
            relationInfo = new UserRelationInfo();
            long relationId = IdHelper.GetLongId();
            relationInfo.UserRelationId = relationId;
            relationInfo.NormalUserPassportId = Convert.ToInt64(nomalUserPassportId);
            relationInfo.AngelUserPassportId = Convert.ToInt64(angelUserPassportId);
            relationInfo.Status = 1;
            relationInfo.Relationtype = 1;
            relationInfo.DateCreated = DateTime.Now;
            BLL.BUserRelation.getInstance().Insert(relationInfo);
        }
        Response.Redirect("MemberRelation.aspx");
    }
    protected void RelationInfoDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        this.RelationInfoDataGrid.CurrentPageIndex = e.NewPageIndex;
        BindRelationInfo();
    }
    protected void RelationInfoDataGrid_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        long userRelationId = Convert.ToInt64(e.Item.Cells[0].Text);
        UserRelationInfo userRelationInfo = BLL.BUserRelation.getInstance().GetRelationInfoByID(userRelationId);
        // 0表示删除，2表示拉黑（未完成）
        userRelationInfo.Status = 0;
        BLL.BUserRelation.getInstance().Update(userRelationInfo);
        BindRelationInfo();
    }
}